<?php
/**
 * Container of user credentials
 *
 * @author Lukas Normantas
 */
class Areal_Auth_Identity implements Zend_Auth_Adapter_Interface
{    
    protected $_email;

    protected $_role;

    protected $_password;

    protected $_user_id;

    public function setEmail($email)
    {
        $this->_email = $email;
    }

    public function setPassword($password)
    {
        $this->_password = $password;
    }

    public function setRoleId($role)
    {
        $this->_role = $role;
    }

    public function setUserId($user_id)
    {
        $this->_user_id = $user_id;
    }

    public function getRoleId()
    {
        return $this->getUser()->role;
    }

    public function getUserId()
    {
        return $this->_user_id;
    }

    public function getUser()
    {
        return Doctrine::getTable('Default_Model_User')->find($this->getUserId());
    }


    public function authenticate()
    {
        $q = Doctrine_Query::create()
            ->select('u.email, u.password, u.id')
            ->from('Default_Model_User u')
            ->where('u.email= ?')
            ->andWhere('u.password = ?')
            ->limit(1);
        $user = $q->fetchOne(array($this->_email,$this->_password));
        if ( ! $user) {
            // failed
            $result = new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE_UNCATEGORIZED,
                null
            );
        } else {
            // passed
            $identity = new Areal_Auth_Identity();
            $identity->setRoleId($user->role);
            $identity->setEmail($user->email);
            $identity->setUserId($user->id);
            $result = new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
        }
        return $result;
    }
}